﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Snowdreamist.Text.UBB
{
    public enum FloatType
    {
        Left,
        Right   // default
    }

    internal class FloatFactory : PrefixUBBElementFactory
    {
        public FloatFactory() : base(ElementPrefix) { }

        public override UBBElement Create(UBBContent ubbContent)
        {
            FloatType type;

            var matchResult = ElementRegex.Match(ubbContent.Text);

            if (!Enum.TryParse<FloatType>(matchResult.Groups[1].Value, true, out type))
                return null;

            return new FloatElement(type);
        }

        #region private

        private readonly static string ElementPrefix = "float";

        private readonly static Regex ElementRegex = new Regex("float=(left|right)", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

        #endregion private
    }

    public class FloatElement : UBBElement
    {
        public const string Label = "float";

        /// <summary>
        /// ctor
        /// </summary>
        public FloatElement() : this(FloatType.Right) { }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="type"></param>
        public FloatElement(FloatType type)
            : base(Label)
        {
            this.Type = type;
        }

        public FloatType Type { get; private set; }

        public override string SerializeToUBBCode()
        {
            return Label + "=" + this.Type.ToString();
        }

        public override object Clone()
        {
            return new FloatElement(this.Type);
        }
    }
}